﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Client.Service;
using System.Collections;
using GenericModels;
using System.Drawing.Printing;

namespace Client
{
    public partial class UserCtlReceipt : UserControl
    {
        private IBooking bookingService;
        private IPerson personService;
        private Booking foundbooking;
        public Booking booking { get; set; }
        public Customer customer { get; set; }
        public Employee employee { get; set; }
        public delegate void UserControlClickHandler(object sender, EventArgs e);   //must be entered first, or 
        public UserControlClickHandler InnerButtonClickNewPartBooking;              //UserControlClickHandler will not be recognized 
        public UserControlClickHandler InnerButtonClickNewBooking;



        public UserCtlReceipt(Booking booking, Customer cus, Employee employee)
        {
            this.booking = booking;
            this.customer = cus;
            this.employee = employee;
            InitializeComponent();
            PrintCustomerInfo();
            bookingService = new BookingClient("WSHttpBinding_IBooking");
            personService = new PersonClient("WSHttpBinding_IPerson");

            LoadBookingInfo();
            LoadTicketInfo();
        }

        private void PrintCustomerInfo()
        {
            txtBoxCustomer.Text = "Kundeoplysninger" + "\r\n" + "\r\n" +
                                    "Navn:      " + customer.FName + "\r\n" + "\r\n" +
                                    "Adresse:   " + customer.Address + "\r\n" + "\r\n" +
                                    "By:        " + customer.City + "Postnr:    " + customer.PostalCode + "\r\n" + "\r\n" +
                                    "Email:     " + customer.Email + "\r\n" + "\r\n" +
                                    "Tlf nr:    " + customer.PhoneNo;

            //Test for succes (spoiler alert: succes!)

            //labInputPhone.Text = booking.PbList.Count.ToString();       //test at alle pb er med.
        }

        private void buttonNew_Click(object sender, EventArgs e)
        {
            if (this.InnerButtonClickNewPartBooking != null)
            {

                this.InnerButtonClickNewPartBooking(sender, e);
            }
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            printDocument();
        }

        /// <summary>
        /// Grabs the employee object and returns the user to the customerscreen
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void newBooking_Click(object sender, EventArgs e)
        {
            if (this.InnerButtonClickNewBooking != null)
            {
                employee = personService.GetEmployeeById(booking.EmpId);
                this.InnerButtonClickNewBooking(sender, e);
            }
        }




        public void printDocument()
        {

            PrintDialog printDialog = new PrintDialog(); //Opens a window where you can select a printer
            PrintDocument printDocument = new PrintDocument(); //Creates a new Document
            printDialog.Document = printDocument; //Attaches the the print window to the dokument
            printDocument.PrintPage += new PrintPageEventHandler(printDocument_PrintPage); // Creates an event for what there is to print
            DialogResult result = printDialog.ShowDialog(); //Returns the result and shows the result
            if (result == DialogResult.OK) //If 
            {
                printDocument.Print(); //When clicked it will open the eventhandler
            }
        }

        private void printDocument_PrintPage(object sender, PrintPageEventArgs e)
        {


            Graphics graphic = e.Graphics; //Creates a new graphic object
            Font font = new Font("Courier New", 12); //Creates a font type and font size
            float fontheight = font.GetHeight();
            int startx = 10; //Where it starts on the x-axis
            int starty = 10; //Where it starts on the y-axis
            int offset = 40; //

            graphic.DrawString("Biografbillet", new Font("Courier New", 18), new SolidBrush(Color.Black), startx, starty); //Creates a Headertext ont the ticket

            for (int i = 0; i <= booking.PbList.Count - 1; i++) //iterates through the ticket count and generates same amount of tickets
            {
                //MessageBox.Show((i).ToString() + " - " + i.ToString());
                string type = booking.PbList[i].TheShow.Type.Title; //Set type title to present Partbooking 
                string room = booking.PbList[i].TheShow.Room.RoomNumber.ToString(); //Set room to present Partbooking roomnumber
                string date = booking.PbList[i].TheShow.Date.ToShortDateString(); //Set date to present Partbooking
                string datetime = booking.PbList[i].TheShow.Date.ToShortTimeString(); // Set time to present Partbooking

                //for (int k = 0; k <= booking.PbList[i].TheShow.Prices.Count - 1; k++)
                //{
                //    string priceb = String.Format("{0:c}", booking.PbList[i].TheShow.Prices[k].TicketPrice.ToString());
                //    graphic.DrawString("Pris : " + priceb, font, new SolidBrush(Color.Black), 10, 130 + starty + offset);
                //    starty += 15; 
                //}

                //Prints data to tickets and sets font-type, x, y, linehight
                graphic.DrawString("Film : " + type, font, new SolidBrush(Color.Black), 10, 10 + starty + offset);
                graphic.DrawString("sal : " + room, font, new SolidBrush(Color.Black), 10, 40 + starty + offset);
                graphic.DrawString("Dato : " + date, font, new SolidBrush(Color.Black), 10, 70 + starty + offset);
                graphic.DrawString("Klokken : " + datetime, font, new SolidBrush(Color.Black), 10, 100 + starty + offset);
                //graphic.DrawString("Pris : " + priceb, font, new SolidBrush(Color.Black), 10, 130 + starty + offset);

                starty += 160; //Adds 160 to the y-axis

            }

        }


        private void LoadBookingInfo()
        {
            lblCusName.Text = customer.FName;
            if (booking == null)
            {
                lblBookId.Text = "0"; //Empty booking object
                lblPartId.Text = "0"; //Empty Partbooking object
            }
            else
            {
                lblBookId.Text = booking.BookingId.ToString();
                PartBooking part = booking.PbList[CurrentPartId()];
                lblPartId.Text = part.Id.ToString();
            }
        }

        /// <summary>
        /// Method to add all the ticket info to the ticket panel
        /// </summary>
        private void LoadTicketInfo()
        {
            lblMovieTitel.Text = booking.PbList[CurrentPartId()].TheShow.Type.Title;
            lblShowroom.Text = booking.PbList[CurrentPartId()].TheShow.Room.RoomNumber.ToString();
            lblRowNo.Text = booking.PbList[CurrentPartId()].TheShow.Room.Row.ToString();
            lblDate.Text = booking.PbList[CurrentPartId()].TheShow.Date.ToShortDateString();

            foreach (Seat s in booking.PbList[CurrentPartId()].Seats)
            {
                listBox1.Items.Add(s.SeatId.ToString());
            }
            lblClock.Text = booking.PbList[booking.PbList.Count - 1].TheShow.Date.ToShortTimeString();

        }

        /// <summary>
        /// Returns the partbooking id for this partbooking
        /// </summary>
        /// <returns></returns>
        private int CurrentPartId()
        {
            return booking.PbList.Count - 1;     //the current show is always the last on the partbooking list
        }
    }
}
